home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / break.def < prev    next >
Text File  |  1993-09-14  |  3KB  |  111 lines

  1. This file is break.def, from which is created break.c.
  2. It implements the builtins "break" and "continue" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES break.c
  23.  
  24. $BUILTIN break
  25. $FUNCTION break_builtin
  26. $SHORT_DOC break [n]
  27. Exit from within a FOR, WHILE or UNTIL loop.  If N is specified,
  28. break N levels.
  29. $END
  30.  
  31. #include "../shell.h"
  32.  
  33. extern char *this_command_name;
  34.  
  35. static int check_loop_level ();
  36.  
  37. /* The depth of while's and until's. */
  38. int loop_level = 0;
  39.  
  40. /* Non-zero when a "break" instruction is encountered. */
  41. int breaking = 0;
  42.  
  43. /* Non-zero when we have encountered a continue instruction. */
  44. int continuing = 0;
  45.  
  46. /* Set up to break x levels, where x defaults to 1, but can be specified
  47.    as the first argument. */
  48. break_builtin (list)
  49.      WORD_LIST *list;
  50. {
  51.   int newbreak;
  52.  
  53.   if (!check_loop_level ())
  54.     return (EXECUTION_FAILURE);
  55.  
  56.   newbreak = get_numeric_arg (list);
  57.  
  58.   if (newbreak <= 0)
  59.     return (EXECUTION_FAILURE);
  60.  
  61.   if (newbreak > loop_level)
  62.     newbreak = loop_level;
  63.  
  64.   breaking = newbreak;
  65.  
  66.   return (EXECUTION_SUCCESS);
  67. }
  68.  
  69. $BUILTIN continue
  70. $FUNCTION continue_builtin
  71. $SHORT_DOC continue [n]
  72. Resume the next iteration of the enclosing FOR, WHILE or UNTIL loop.
  73. If N is specified, resume at the N-th enclosing loop.
  74. $END
  75.  
  76. /* Set up to continue x levels, where x defaults to 1, but can be specified
  77.    as the first argument. */
  78. continue_builtin (list)
  79.      WORD_LIST *list;
  80. {
  81.   int newcont;
  82.  
  83.   if (!check_loop_level ())
  84.     return (EXECUTION_FAILURE);
  85.  
  86.   newcont = get_numeric_arg (list);
  87.  
  88.   if (newcont <= 0)
  89.     return (EXECUTION_FAILURE);
  90.  
  91.   if (newcont > loop_level)
  92.     newcont = loop_level;
  93.  
  94.   continuing = newcont;
  95.  
  96.   return (EXECUTION_SUCCESS);
  97. }
  98.  
  99. /* Return non-zero if a break or continue command would be okay.
  100.    Print an error message if break or continue is meaningless here. */
  101. static int
  102. check_loop_level ()
  103. {
  104. #if defined (BREAK_COMPLAINS)
  105.   if (!loop_level)
  106.     builtin_error ("Only meaningful in a `for', `while', or `until' loop");
  107. #endif /* BREAK_COMPLAINS */
  108.  
  109.   return (loop_level);
  110. }
  111.